﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Atmk.WaterMeter.MIS.Base;
using Atmk.WaterMeter.MIS.Common;
using Atmk.WaterMeter.MIS.Commons;
using Atmk.WaterMeter.MIS.Commons.Interfaces.Logic;
using Atmk.WaterMeter.MIS.Commons.Interfaces.Logic.Statistics;
using Atmk.WaterMeter.MIS.Commons.Utils;
using Atmk.WaterMeter.MIS.Commons.ViewModels;
using Atmk.WaterMeter.MIS.Commons.ViewModels.OwnerMeter;
using Atmk.WaterMeter.MIS.Datas;
using Atmk.WaterMeter.MIS.Entities;
using Atmk.WaterMeter.MIS.Entities.Enums;
using Microsoft.AspNetCore.Mvc;
using NLog;

namespace Atmk.WaterMeter.MIS.Controllers
{
    [Authorize]
    public class MeterController : BaseApiController
    {
        protected Logger _logger = LogManager.GetCurrentClassLogger();
        private readonly TokenHelper _tokenHelper;
        private readonly IBaseInfoLogic _baseStatistcsLogic;
        private readonly IOwnerMeterLogic _ownerMeterLogic;

        /// <inheritdoc />
        public MeterController(
            IBaseInfoLogic baseInfoLogic,
            IOwnerMeterLogic ownerMeterLogic,
            TokenHelper tokenHelper)
        {
            _baseStatistcsLogic = baseInfoLogic;
            _ownerMeterLogic = ownerMeterLogic;
            _tokenHelper = tokenHelper;
        }


        /// <summary>
        /// 获取水表类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMeter([FromBody] dynamic obj)
        {
            try
            {
                var token = _tokenHelper.GetTokenByHttpContext();
                if (obj == null)
                {
                    _logger.Debug("传入Josn数据为空");
                    return Ok(new { errcode = 1, msg = $"传入数据出错" });
                }
                var result = _ownerMeterLogic.InsertMeter(obj.districtId.ToString(), obj.owner, obj.waterMeter, token, out string ErrorMessage);

                return Ok(result
                ? new { errcode = 0, msg = "ok" }
                : new { errcode = 1, msg = ErrorMessage });
            }
            catch (Exception e)
            {
                _logger.Error(e.Message);
                return Ok(new
                {
                    errcode = 1,
                    msg = $"添加信息失败"
                });
            }
        }

    }
}